package com.sf;

import cn.hutool.http.HttpRequest;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sf.entity.SFPushStatus;
import com.sf.entity.SFTrailerParam;
import com.sf.util.SFMd5SignUtil;
import lombok.var;

import java.io.FileInputStream;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class PushSFAlarmTypeStatusData {

    static Map<Integer, String> translationLocation = new HashMap<>();
    static {
        translationLocation.put(2, "L1");
        translationLocation.put(3, "R1");
        translationLocation.put(6, "L2");
        translationLocation.put(7, "R2");
        translationLocation.put(10, "L3");
        translationLocation.put(11, "R3");
    }

    /**
     * select
     *   pressure_sensor_id sensorNo,
     *   license_plate carrierNo,
     *   receipt_Time upTime,
     *   gmt_Create initDate,
     *   is_receive_data_timeout sensorStatus
     * from
     *   device_tyre_pressure_last
     * WHERE license_plate in  (select license_plate FROM `sf_license_plate_config`)
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        //pro
        String appSecret = "opW79kLM78Sp";
        String url ="https://gis-rss-scm-stsoc.sf-express.com/stsoc";

        //test
//        String appSecret = "1pJkS09dNn6tt";
//        String url ="https://gis-rss-scm-stsoc.sit.sf-express.com:48020/stsoc";


        // 创建一个输入流，将 Excel 文件读取出来
        InputStream inputStream = new FileInputStream("C:\\Users\\hcf\\Desktop\\解除告警.xlsx");

        List<SFTrailerParam> tmpList = EasyExcel.read(inputStream)
                // 设置与Excel表映射的类
                .head(SFTrailerParam.class)
                // 设置sheet,默认读取第一个
                .sheet()
                // 设置标题所在行数
                .headRowNumber(1)
                // 异步读取
                .doReadSync();



        for (SFTrailerParam sfTrailerParam : tmpList) {

            Map<String, String> params = buildParams();
            String sign = SFMd5SignUtil.signRequest(params, appSecret);
            params.put("sign", sign);

            SFTrailerParam trailerData = createTrailerData(sfTrailerParam);
            trailerData.setAlarmType("2");

            List<List<SFTrailerParam>> requestData = Collections.singletonList(Collections.singletonList(trailerData));
            String request = JSON.toJSONString(requestData);

            String response = HttpRequest.post(url
                            + "/openapi/pushAlarm").addHeaders(params).contentType("application/json") // 修改内容类型为 JSON
                    .body(request).execute().body();

            System.out.println("推送顺丰共建轴温告警返回,车牌号: "+sfTrailerParam.getCarrierNo()+", 请求参数:"+request+", result: "+response );
        }




    }


    private static SFTrailerParam createTrailerData(SFTrailerParam trailerData) {


        trailerData.setWheelPosition(translationLocation.get(Integer.valueOf(trailerData.getWheelPosition())));

        // 转换 receiptTime 和 daqTime 到毫秒级时间戳
        long receiptTimeMillis = System.currentTimeMillis();
        // 设置 trailerData 的时间戳属性
        trailerData.setTempTime(String.valueOf(receiptTimeMillis
                / 1000));
        trailerData.setGpsTime(String.valueOf(receiptTimeMillis
                / 1000));

        trailerData.setCreateTime(System.currentTimeMillis()
                / 1000
                + "");



        trailerData.setSpeed(0.0);
        return trailerData;
    }


    // 构建公共参数
    private static Map<String, String> buildParams() {
        Map<String, String> params = new HashMap<>();
        params.put("appid", "DCDZ");
        params.put("timestamp", System.currentTimeMillis()
                + "");
        return params;
    }



}
